########################################################################
######################### PROGRAMAS E PACOTES ##########################
########################################################################
############################################################# March/2024

## Dr.Neander M. Heming - PPGECB - UESC
## www.researchgate.net/profile/Neander_Heming
## neanderh@yahoo.com.br
##===========================================================##
## 
## 


# 1
####--- Instalar programas ---####:
#### R: https://cran.r-project.org/
#### RStudio: https://www.rstudio.com/products/rstudio/download/
#### JAVA jdk: https://www.oracle.com/java/technologies/javase-jdk14-downloads.html
#### MaxEnt: http://www.cs.princeton.edu/~schapire/maxent/

#### Rtools (Windows apenas)
install.packages("Rtools")
install.packages("installr")

# You will probably get stuck trying to install Rtools. So, use installr to install Rtools:
library(installr)
install.Rtools()
# Choose the latest version and let it download and install.
# A window will probably appear. Click to advance and install everything. If the window 
# to choose version does not appear, enter the website the warning message tells you
# to access (https://cran.r-project.org/bin/windows/Rtools/).
# You can download and install Rtools from there.

# 2
####--- Baixar e instalar pacotes do R ---####:
# Especificar lista de pacotes para baixar
pkgs <- c("sp", "raster", "spThin", "rJava", 
          "dismo", "devtools") 

pkgs <- pkgs[!pkgs %in% installed.packages()[,1]]
# Instalar pacotes
install.packages(pkgs)

library(devtools)
# devtools::install_github("cran/GISTools") # se não der não tem problema]
devtools::install_github("marlonecobos/kuenm")
devtools::install_github("HemingNM/ENMeval", ref="NMH")
devtools::install_github("HemingNM/ENMwizard")
devtools::install_github("johnbaums/rmaxent")


# 3
#### --- Obter o maxent e colocar na pasta do dismo ---####:
## 3a
rmaxent::get_maxent()

## ou 3b
#### Colocar arquivo 'maxent.jar' (que está no arquivo comprimido "maxent.zip") dentro do diretório 'java' do pacote 'dismo'.
# diretório 'java' do pacote 'dismo'
d <- system.file(package='dismo', 'java')
# download pelo link
u <- 'https://biodiversityinformatics.amnh.org/open_source/maxent/maxent.php?op=download'
utils::download.file(u, f <- paste0(getwd(),"/maxent.zip"), mode='wb')
# extração do arquivo no diretório 'java' do pacote 'dismo'
# utils::unzip(f, exdir=d, list=T, files='maxent.jar', junkpaths=TRUE)
utils::unzip(f, exdir=d, files='maxent/maxent.jar', junkpaths=TRUE)

# ou 3c
# Download manual pro diretório de trabalho
# diretório 'java' do pacote 'dismo'
d <- system.file(package='dismo', 'java')
# localização do maxent.zip no computador
f <- file.choose()
# ou
# f <- paste0(getwd(),"/maxent.zip")
# extração do arquivo no diretório 'java' do pacote 'dismo'
utils::unzip(f, 
             exdir=d, 
             files='maxent/maxent.jar', junkpaths=TRUE)


# 4
#### --- Confirmar se está tudo funcionando ---####:
list.files(system.file(package='dismo', 'java')) # o arquivo maxent.jar está listado nesta pasta?
library(rJava)
library(dismo)
maxent()
# This is MaxEnt version x.x.x

# Se apareceu a mensagem "This is MaxEnt version x.x.x" está tudo pronto!!



### Somente leia abaixo se tiver algum erro.

## Usuários MacOS: se tiverem erro ao rodar o maxent(), procurem ajuda em:
# https://groups.google.com/forum/#!topic/maxent/lbdWcnmTc5A
# http://stackoverflow.com/questions/30738974/rjava-load-error-in-rstudio-r-after-upgrading-to-osx-yosemite


## Usuários Windows:
## Problemas para compilar pacotes:
# baixar Rtools 4.0: https://cran.r-project.org/bin/windows/Rtools/
# put the location of the Rtools make utilities (bash, make, etc) on the PATH:
writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")
# restart R, and verify that make can be found, which should show the path to your Rtools installation.
Sys.which("make")
## "C:\\rtools40\\usr\\bin\\make.exe"
# If this works, you can try to install an R package from source

## Usuários Mac:
# https://mac.r-project.org/tools/

search()
library(ENMeval)
library(ENMwizard)
